var app = getApp();
var year,range,yearArr;
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year:"",
    yearArr:[],
    obj:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    range=3;
    year =String(new Date().getFullYear()) 
    yearArr=[year];
    for(let i=1;i<range;i++){
      yearArr.push(String(1*year+i));
      yearArr.unshift(String(year-i));
    }
    this.setData({
      year : year,
      yearArr:yearArr,
    })
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectYear(){
    var that=this;
    wx.showActionSheet({
      itemList: that.data.yearArr,
      success (res) {
        that.setData({
          year:yearArr[res.tapIndex]
        })
        that.getInfo()
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  getInfo(){
    var that =this;
    wx.showLoading({
      title: '请求中',
    })
    wx.request({
      url: app.getUrl() + '/api/order_info/saleinfo_summary',
      data: {
        mallSaleManId: app.globalData.mallSaleMan.mallSaleManId,
        startYear: this.data.year,
        endYear: this.data.year,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        wx.hideLoading({
          success: (res) => {},
        })
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          that.setData({
            obj:res.data.obj
          })
        } else {
          //token他处登录
          if (res.data.status == 403) {
            wx.showModal({
              title: '警告',
              content: '您的用户已在别的地方登录，是否重新登录？',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../login/login',
                  })
                } else { }
              }
            })
          }
          // 没有登录状态
          else if (res.data.status == 406) {
            wx.switchTab({
              url: '../vipInfo/vipInfo',
              complete() {
                wx.showToast({
                  title: '您还没有登录，请登录后查看',
                  icon: "none",
                  duration: 3000
                })
              }
            })
          } else {
            $Toast({
              content: res.data.msg,
              type: 'warning',
              duration: 2,
            });
          }
        }
      },
      fail(res) {
        console.log(res)
        $Toast({
          content: '连接服务器失败',
          type: 'error',
          duration: 2,
        });
      }
    })
  },
  choose(e){
    wx.navigateTo({
      url: '../xsjlDetail/xsjlDetail?month='+e.currentTarget.dataset.month,
    })
  }
})